﻿using System;
using System.Net;
using System.Net.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tavis.Http;

namespace Tavis {
    [TestClass]
    public class HttpClientTests {


        //[Ignore] // 
        //[TestMethod]
        //public void GetTheWeather() {
        //    var httpClient = new HttpClient();
        //    var response = httpClient.Get("http://www.google.com/ig/axpi?weather=Montreal");
        //    //var xElement = response.Content. ReadAs<XElement>(new XContentFormatter());
        //    //var currentConditon = xElement.XPathSelectElement("//current_conditions/condition");
        //    var streamContent = response.Content;
        //    //Assert.IsFalse(String.IsNullOrEmpty(currentConditon.Attribute("data").Value));
        //}


        //[Ignore]
        //[TestMethod]
        //public void GetTheWeatherAsync() {
        //    var httpClient = new HttpClient();
        //    var requestTask = httpClient.GetAsync("http://www.google.com/ig/api?weather=Montreal");
        //    XElement currentConditon = null;
        //    requestTask.ContinueWith((task) => {
        //                        var response = task.Result;
        //                        var xElement = response.Content.ReadAsObject<XElement>(new XContentFormatter());
        //                        currentConditon = xElement.XPathSelectElement("//current_conditions/condition");
        //            });
            
        //    Thread.Sleep(1000);
        //    Assert.IsFalse(String.IsNullOrEmpty(currentConditon.Attribute("data").Value));
        //}

        [Ignore]
        [TestMethod]
        public void PostSomeContent() {
            var httpClient = new HttpClient();
            var content = new StringContent("This is some plain text content");
            var response = httpClient.PostAsync("http://localhost:9000/MessageProcessor",content).Result;
            
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }





        [TestMethod]
        public void TestUrl() {
            var url = new Uri("http://localhost:8000/" + HttpUtility.UrlEncode("this/does/not/work"));
            Assert.AreEqual(url.AbsoluteUri, "http://localhost:8000/this/does/not/work");
        }

        [TestMethod]
        public void TestUrlUsingEscapedQueryString() {
            var url = new Uri("http://localhost:8000/foo?" + HttpUtility.UrlEncode("this/does/not/work"));
            Assert.AreEqual(url.AbsoluteUri, "http://localhost:8000/foo?this%2fdoes%2fnot%2fwork");
        }
    }
}
